<script setup>
import { ref } from 'vue'

  const valueX = ref('')
  const valueZ = ref('')
  let msgA = ref('')
  let msgB = ref('')
  let msgC = ref('')
  let msgD = ref('')

  //定义车辆长、宽
  const Car = {
    width : 315,
    height: 615
  }
  const normalVector = [1,0,0]; // Normal vector of plane (x, z)
  //x,y,z
  const pointA = [0 - parseInt(Car.width/2), 0, parseInt(Car.height)]
  const pointB = [parseInt(Car.width/2), 0, parseInt(Car.height)]
  const pointC = [0 - parseInt(Car.width/2), 0, 0]
  const pointD = [parseInt(Car.width/2), 0, 0]

  //点击button
  function computeValue() {
    const x1Degrees = valueX.value; // Rotation around x-axis in degrees
    const z1Degrees = valueZ.value; // Rotation around z-axis in degrees
    let point = pointA;
    let outPoint = rotatePoint(point, x1Degrees, z1Degrees);
    let distance = distanceToPlane(outPoint);
    msgA.value = JSON.stringify(outPoint) + ', distance='+distance;

    point = pointB;
    outPoint = rotatePoint(point, x1Degrees, z1Degrees);
    distance = distanceToPlane(outPoint);
    msgB.value = JSON.stringify(outPoint) + ', distance='+distance;

    point = pointC;
    outPoint = rotatePoint(point, x1Degrees, z1Degrees);
    distance = distanceToPlane(outPoint);
    msgC.value = JSON.stringify(outPoint) + ', distance='+distance;

    point = pointD;
    outPoint = rotatePoint(point, x1Degrees, z1Degrees);
    distance = distanceToPlane(outPoint);
    msgD.value = JSON.stringify(outPoint) + ', distance='+distance;
  }

  //右手螺旋法则
  function rotatePoint(point, xAngle, zAngle) {
    const x1 = point[0],y1 = point[1],z1 = point[2];
    // 首先，将角度转换为弧度
    const xRad = xAngle * Math.PI / 180;
    const zRad = zAngle * Math.PI / 180;

    // 计算绕x轴旋转后的中间点a'
    const xPrime = x1;
    const yPrime = y1 * Math.cos(xRad) - z1 * Math.sin(xRad);
    const zPrime = y1 * Math.sin(xRad) + z1 * Math.cos(xRad);

    // 然后，计算绕z轴旋转后的最终点a2
    const x2 = xPrime * Math.cos(zRad) - yPrime * Math.sin(zRad);
    const y2 = xPrime * Math.sin(zRad) + yPrime * Math.cos(zRad);
    const z2 = zPrime;

    // 返回最终点a2的坐标
    return { x: x2.toFixed(2), y: y2.toFixed(2), z: z2.toFixed(2) };
  }

  

  function distanceToPlane(point) {
    // Calculate the distance
    const pointVector = [point.x, point.y, point.z];
    const distance = Math.abs(pointVector[1] * normalVector[0]) / Math.sqrt(normalVector[0] * normalVector[0] + normalVector[1] * normalVector[1] + normalVector[2] * normalVector[2])
    return distance
  }
  
  function dotProduct(vector1, vector2) {
    if (vector1.length !== vector2.length) {
      throw new Error('Vector lengths must be equal');
    }

    let product = 0;
    for (let i = 0; i < vector1.length; i++) {
      product += vector1[i] * vector2[i];
    }

    return product;
  }
</script>

<template>
  <div>
    <text>右手螺旋法则，长：{{ Car.height }}, 宽：{{ Car.width }}</text> <br>
    <text>位置：A={{ pointA }},B={{ pointB }},C={{ pointC }},D={{ pointD }}</text>
    <br>
  </div>
  <br>
  <div>
    旋转角度:<br>
    x:<input v-model="valueX"/>
    z:<input v-model="valueZ"/>
    <button @click="computeValue">计算</button>
  </div>
  <br>
  <br>
  <div>
    点到平面(x,z)的距离:<br>
    A: {{ msgA }}<br>
    B: {{ msgB }}<br>
    C: {{ msgC }}<br>
    D: {{ msgD }}<br>
  </div>
</template>

<style scoped>

</style>
